package com.github.cyf.controller.v1;

import com.github.cyf.common.module.Rs;
import com.github.cyf.entity.FileResp;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenyifan
 * @create 2024-03-26 15:49
 */
@RestController
@Api(tags = "前台Api接口")
@ApiSupport(order = 1)
@RequestMapping("/api")
public class ApiController {

    /**
     * position 参数生效需要开启 knife4j 的增强配置
     */
    @ApiOperation(value = "普通body请求")
    @ApiOperationSupport(order = 3)
    @PostMapping("/body")
    public Rs body(@RequestBody FileResp fileResp){
        return Rs.ok().setData(fileResp);
    }

    /**
     * paramType 参数值： {@link springfox.documentation.service.ParameterType}
     */
    @ApiOperation(value = "普通body请求+Param+Header+Path")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件id", paramType = "path", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "token", value = "请求token", paramType = "header", required = true),
            @ApiImplicitParam(name = "name", value = "文件名称", paramType = "query", required = false)
    })
    @PostMapping("/bodyParamHeaderPath/{id}")
    public Rs bodyParamHeaderPath(@PathVariable("id") String id,
                                  @RequestHeader("token") String token,
                                  @RequestParam("name") String name,
                                  @RequestBody FileResp fileResp){
        return Rs.ok().set("id", id).set("token", token).set("name", name).set("fileResp", fileResp);
    }

    @PostMapping("/uploadOneFile")
    @ApiOperation(value = "单文件上传")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传的文件", dataTypeClass = MultipartFile.class, required = true),
            @ApiImplicitParam(name = "group", value = "group", required = true)
    })
    public Rs uploadOneFile(@RequestPart("file") MultipartFile file,
                            @RequestParam(value = "group", required = false) String group) {
        return Rs.ok().set("fileName", file.getOriginalFilename()).set("group", group);
    }

    /**
     * 文档管理 -> 个性化设置 -> 开启动态请求参数
     * 开启动态请求之后会有一个自定义的框框，添加多个 files 即可
     * @param files
     * @param group
     * @return
     */
    @PostMapping("/uploadMultipleFile")
    @ApiOperation(value = "多文件上传")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "上传的文件", dataTypeClass = MultipartFile.class, required = true),
            @ApiImplicitParam(name = "group", value = "group", required = true)
    })
    public Rs uploadMultipleFile(@RequestPart("files") MultipartFile[] files,
                                 @RequestParam(value = "group", required = false) String group) {
        List<String> fileNameList = Arrays.stream(files)
                .map(MultipartFile::getOriginalFilename)
                .collect(Collectors.toList());
        return Rs.ok().set("fileNameList", fileNameList).set("group", group);
    }

}
